package com.example.demo.manhua;

import java.util.Arrays;

/**
 * @ClassName : MaxValueAdd
 * @Description : 给出两个很大的整数，要求实现程序求出两个整数之和。
 * @Author : wind
 * @Date: 2021-01-29 15:40
 * @Version 1.0
 */
public class MaxValueAdd {

    public static void main(String[] args) {
        System.out.println(add("426709752318", "95481253129"));
        System.out.println(add("95", "5"));
    }

    // 填充数组
    static String add(String num1, String num2) {
        int maxLength = num1.length() > num2.length() ? num1.length() : num2.length();
        int[] array1 = new int[maxLength + 1];
        int[] array2 = new int[maxLength + 1];
        int[] result = new int[maxLength + 1];
        int[] filling1 = filling(num1, array1);
        int[] filling2 = filling(num2, array2);
        System.out.println(Arrays.toString(filling1));
        System.out.println(Arrays.toString(filling2));
        int temp = 0;
        for (int i = 0; i < filling1.length; i++) {
            int res = filling1[i] + filling2[i] + temp;
            if (res < 10) {
                result[i] = res;
                temp = 0;
            } else {
                result[i] = res - 10;
                temp = 1;
                if (result[i] == 10) {
                    result[i] = 0;
                    temp += 1;
                }
                if (result[i] > 10) {
                    result[i] = result[i] - 10;
                    temp += 1;
                }
            }
        }
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < result.length; i++) {
            sb.append(result[result.length - 1 - i]);
        }
        if (sb.toString().indexOf("0") == 0) {
            sb.delete(0, 1);
        }
        return sb.toString();
    }

    // 填充数组
    static int[] filling(String str, int array[]) {
        char[] chars = str.toCharArray();
        //123456
        for (int i = 0; i < chars.length; i++) {
            array[str.length() - 1 - i] = Integer.parseInt((char) chars[i] + "");
        }
        return array;
    }


}
